package com.pandora.framework.util;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.pandora.common.util.StringUtils;
import com.pandora.framework.constant.Constants;
import com.pandora.framework.constant.ServletConstants;

/**
 * 配置文件加载工具
 * 
 * @author JingChenglong 2017/12/16 12:49
 *
 */
public class PropertiesUtils {

	protected static final String BUNDLE_NAME = "properties.";// 配置文件根节点
	protected static final String PREFIX = (StringUtils.isNotEmpty(ServletConstants.ENVIRONMENT)
			? ServletConstants.ENVIRONMENT + "." : "");

	private static final String BUNDLE_NAME_ACCESS = BUNDLE_NAME + PREFIX + "access";// 通行证
	private static final String BUNDLE_NAME_BUSMSG = BUNDLE_NAME + PREFIX + "business_message";// 业务错误信息
	private static final String BUNDLE_NAME_MSG = BUNDLE_NAME + PREFIX + "business";// 系统默认错误信息
	private static final String BUNDLE_NAME_COMMON_MSG = BUNDLE_NAME + PREFIX + "common_message";// 系统业务配置信息
	private static final String BUNDLE_NAME_CONFIG = BUNDLE_NAME + PREFIX + "config";// 通用配置文件
	private static final String BUNDLE_NAME_DB = BUNDLE_NAME + PREFIX + "db";// 数据源
	private static final String BUNDLE_NAME_IP_LIMIT = BUNDLE_NAME + PREFIX + "iplimit";// IP限制
	private static final String BUNDLE_NAME_LOG = BUNDLE_NAME + PREFIX + "log";// 日志
	private static final String BUNDLE_NAME_THIRDPARTY = BUNDLE_NAME + PREFIX + "thirdparty";// 第三方平台相关配置

	private static ResourceBundle RESOURCE_BUNDLE_ACCESS = null;
	private static ResourceBundle RESOURCE_BUNDLE_BUSMSG = null;
	private static ResourceBundle RESOURCE_BUNDLE_MSG = null;
	private static ResourceBundle RESOURCE_BUNDLE_COMMON_MSG = null;
	private static ResourceBundle RESOURCE_BUNDLE_CONFIG = null;
	private static ResourceBundle RESOURCE_BUNDLE_DB = null;
	private static ResourceBundle RESOURCE_BUNDLE_IP_LIMIT = null;
	private static ResourceBundle RESOURCE_BUNDLE_LOG = null;
	private static ResourceBundle RESOURCE_BUNDLE_THIRDPARTY = null;

	public static void init() {
		RESOURCE_BUNDLE_ACCESS = ResourceBundle.getBundle(BUNDLE_NAME_ACCESS);
		RESOURCE_BUNDLE_BUSMSG = ResourceBundle.getBundle(BUNDLE_NAME_BUSMSG);
		RESOURCE_BUNDLE_MSG = ResourceBundle.getBundle(BUNDLE_NAME_MSG);
		RESOURCE_BUNDLE_COMMON_MSG = ResourceBundle.getBundle(BUNDLE_NAME_COMMON_MSG);
		RESOURCE_BUNDLE_CONFIG = ResourceBundle.getBundle(BUNDLE_NAME_CONFIG);
		RESOURCE_BUNDLE_DB = ResourceBundle.getBundle(BUNDLE_NAME_DB);
		RESOURCE_BUNDLE_IP_LIMIT = ResourceBundle.getBundle(BUNDLE_NAME_IP_LIMIT);
		RESOURCE_BUNDLE_LOG = ResourceBundle.getBundle(BUNDLE_NAME_LOG);
		RESOURCE_BUNDLE_THIRDPARTY = ResourceBundle.getBundle(BUNDLE_NAME_THIRDPARTY);
	}

	public static String getLog(String key) {
		return getString(RESOURCE_BUNDLE_LOG, key);
	}

	public static String getDb(String key) {
		return getString(RESOURCE_BUNDLE_DB, key);
	}

	public static String getConfig(String key) {
		return getString(RESOURCE_BUNDLE_CONFIG, key);
	}

	public static String getAccess(String key) {
		return getString(RESOURCE_BUNDLE_ACCESS, key);
	}

	public static String getIpLimit(String key) {
		return getString(RESOURCE_BUNDLE_IP_LIMIT, key);
	}

	public static String getBusinessMessage(String key) {
		return getString(RESOURCE_BUNDLE_BUSMSG, key);
	}

	public static String getMessage(String key) {
		return getString(RESOURCE_BUNDLE_MSG, key);
	}

	public static String getCommonMessage(String key) {
		return getString(RESOURCE_BUNDLE_COMMON_MSG, key);
	}

	public static String getThirdParty(String key) {
		return getString(RESOURCE_BUNDLE_THIRDPARTY, key);
	}

	public static String getString(ResourceBundle resourceBundle, String key) {

		if (resourceBundle == null || StringUtils.isEmpty(key)) {
			return Constants.UNKNOWN;
		}

		try {
			return StringUtils.nullToStrTrim(resourceBundle.getString(key));
		} catch (MissingResourceException e) {
			return Constants.UNKNOWN;
		}
	}

	public static void cleanCache() {
		ResourceBundle.clearCache();
	}
}